/*
 *    Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC

 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import prompt from '@system.prompt'
import router from '@system.router'
import http from '@ohos.net.http'
import util from '@ohos.util'

export default {
    data: {
        oldpwd: '',
        newpwd: '',
        newpwd2: '',
    },
    onInit() {
    },
    onDestroy() {
    },
    getOldPassword(e) {
        this.oldpwd = e.value;
    },
    getNewPassword(e) {
        this.newpwd = e.value;
    },
    getNewPassword2(e) {
        this.newpwd2 = e.value;
    },
    onModify() {
        if (this.oldpwd.length == 0 || this.newpwd.length == 0 || this.newpwd2.length == 0) {
            prompt.showToast({
                message: "密码不能为空"
            })
            return;
        }
        if (this.oldpwd == this.newpwd || this.oldpwd == this.newpwd2) {
            prompt.showToast({
                message: "新旧密码不能相同"
            })
            return;
        }
        if (this.newpwd != this.newpwd2) {
            prompt.showToast({
                message: "两次输入的新密码不一致"
            })
            return;
        }
        let url = this.$app.$def.data.usermgmtIP + '/v1/users/password';
        let httpRequest = http.createHttp();
        httpRequest.on('napi_headersReceive', (data) => {});
        let session_value = 'AUTHSERVERSESSIONID=' + this.$app.$def.data.session + ';';
        let cookie_value = session_value + 'XSRF-TOKEN=' + this.$app.$def.data.token + ';';
        httpRequest.request(
            url,
            {
                method: 'PUT', // 可选，默认为“GET”
                header: {
                    'Content-Type': 'application/json',
                    'Cookie': cookie_value,
                    'Connection': 'keep-alive',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept': '*/*',
                    'X-XSRF-TOKEN': this.$app.$def.data.token
                },
                extraData:
                {
                    "type": 1,
                    "newPassword": this.newpwd,
                    "oldPassword": this.oldpwd,
                },
                connectTimeout: 60000, // 可选，默认为60s
                readTimeout: 60000, // 可选，默认为60s
            },(err, data) => {
                if (!err) {
                    console.info('myapp modify password ok' + data.result);
                    prompt.showToast({
                        message: "密码修改成功",
                    })
                    router.back()
                } else {
                    console.info('myapp modify password error:' + err);
                    prompt.showToast({
                        message: "密码修改失败",
                    })
                }
                httpRequest.destroy();
            }
        );
    },
    onCancel(){
        router.back()
    },
}
